// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// import AudioManage, {soundList} from "../resources/framework/AudioManage";

import AudioManage, {soundList} from "../framework/AudioManage";

const {ccclass, property} = cc._decorator;

export enum doorType {
    normalDoor = 0,
    redKeyDoor = 1,
    blueDoor =  2,
    greenDoor = 3,
    orangeDoor = 4,
    purpleDoor = 5,
    whiteDoor = 6,
    moveDoor = 7
}

@ccclass
export default class doorMediator extends cc.Component {

    @property({displayName : "门移动的距离"})
    moveDistance : cc.Vec2 = cc.v2(0,0);

    @property({
        type : cc.Enum(doorType),
        displayName : "门的类型"
    })
    curDoor : doorType = doorType.normalDoor;

    @property()
    moveUp = true;

    @property()
    notMove  = false;

    @property(cc.Node)
    targetNode : cc.Node = null;

    isCheck : boolean = false;

    initPos = null;

    onLoad(){
        this.initPos = this.node.position;
    }

    doorMoveAction(){
        AudioManage.playSFX(soundList.liftDoor,false);
        // cc.tween(this.node).by(0.5,{
        //     position : this.moveDistance
        // }).start();
        // this.node.getChildByName("spine").active = true;
        if(this.node.getChildByName("pic")){
            this.node.getChildByName("pic").active = false;
        }
        if(this.node.getChildByName("spine")){
            let spk = this.node.getChildByName("spine").getComponent(sp.Skeleton);
            spk.setAnimation(0,"animation",false);
            spk.setCompleteListener(()=>{
                this.node.destroy();
            })
        }else{
            cc.tween(this.node).by(0.5,{
                position : this.moveDistance
            }).start();
        }
    }

    doorOnlyMoveUp(){
        if(!this.moveUp) return;
        this.moveUp = false;
        cc.tween(this.node).by(0.1,{
            position : this.moveDistance
        }).start();
    }

    doorOnlyMoveDown(){
        if(this.moveUp) return;
        this.moveUp = true;
        cc.tween(this.node).by(0.1,{
            position : cc.v2(this.moveDistance.x,-this.moveDistance.y)
        }).start();
    }

    doorMoveUp(event?){
        this.moveUp = !this.moveUp;
        if(this.notMove){
            if(this.moveUp){
                this.scheduleOnce(()=>{
                    this.node.y = this.targetNode.position.y;
                });
            }else{
                this.scheduleOnce(()=>{
                    this.node.y = this.initPos.y;
                });
            }
        }else{
            if(this.moveUp){
                cc.tween(this.node).by(0.1,{
                    position : cc.v2(this.moveDistance.x,-this.moveDistance.y)
                }).start();
            }else{
                cc.tween(this.node).by(0.1,{
                    position : this.moveDistance
                }).start();
            }
        }
    }

    doorMoveLine(){
        this.moveUp = !this.moveUp;
        if(this.moveUp){
            cc.tween(this.node).by(0.1,{
                position : this.moveDistance
            }).start();
        }else{
            cc.tween(this.node).by(0.1,{
                position : cc.v2(-this.moveDistance.x,this.moveDistance.y)
            }).start();
        }
    }

    onBeginContact(contact, self, other) {
        if(this.curDoor != doorType.moveDoor) return;
        if(other.node.group == "role"){
            this.isCheck = true;
        }
    }

    onEndContact(contact, self, other) {
        if(this.curDoor != doorType.moveDoor) return;
        if(other.node.group == "role"){
            this.isCheck = false;
            if(this.isPlay){
                AudioManage.playSFX(soundList.pushDoor,true,true,1);
                this.isPlay = false
            }
        }
    }

    doorOpenJudge(key,callback){
        if(this.curDoor == key){
            this.doorMoveAction();
            callback();
            console.log("开门");
        }else{
            console.log("钥匙不对");
        }
    }

    isPlay = false;
    protected update(dt: number): void {
        if(this.isCheck){
            if(!this.isPlay){
                AudioManage.playSFX(soundList.pushDoor,true,false,1);
                this.isPlay = true;
            }
        }
    }
}
